<?php

namespace App\Http\Controllers;

use App\Http\Requests\TiebaRequest;
use App\Jobs\TiebaJob;
use App\Models\TiebaCrawlerStatus;
use Illuminate\Http\Request;

class TiebaController extends Controller
{
    public function index()
    {
        return view('resources.tieba.index');
    }

    public function store(TiebaRequest $request, TiebaCrawlerStatus $tiebaCrawlerStatus)
    {
        preg_match('/(p\/|f\?kz=)(\d+)/', $request->input('url'), $match);
        $tiebaCrawlerStatus->user_id = $request->user()->id;
        $tiebaCrawlerStatus->post_id = $match[2];
        $tiebaCrawlerStatus->status = $tiebaCrawlerStatus::STATUS_ON_QUEUE;
        $tiebaCrawlerStatus->save();
        TiebaJob::dispatch($tiebaCrawlerStatus->id);

        return redirect()->route('tieba.show', ['id' => $tiebaCrawlerStatus->id]);
    }

    public function show($id)
    {
        return view('resources.tieba.show', compact('id'));
    }

    public function status($id, TiebaCrawlerStatus $tiebaCrawlerStatus)
    {
        return $tiebaCrawlerStatus->findOrFail($id);
    }
}
